package com.scfw.db;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by sehuaqi on 2017/7/2.
 */
@Service
public class AccountService implements IAccountService {

    @Autowired
    IAccountDAO accountDAO;

    @Autowired
    AccountDao accountDao;

    @Autowired
    AccountMapper accountMapper;

    @Autowired
    AccountMapper2 accountMapper2;

    @Override
    public int add(Account account) {
        return accountDAO.add(account);
    }

    @Override
    public int update(Account account) {
        return accountDAO.update(account);
    }

    @Override
    public int delete(int id) {
        return accountDAO.delete(id);
    }

    @Override
    public Account findAccountById(int id) {
        return accountDAO.findAccountById(id);
    }

    @Override
    public List<Account> findAccountList() {
        return accountDAO.findAccountList();
    }

    /************ jpa test ************/

    @Override
    public List<Account> findAll(){
        return accountDao.findAll();
    }

    @Override
    public Account findOne(int id) {
        return accountDao.findOne(id);
    }

    @Override
    public String updateAccount(Account account) {

        account = accountDao.saveAndFlush(account);

        return JSON.toJSONString(account);
    }

    @Override
    public String save(Account account) {

        account = accountDao.save(account);

        return JSON.toJSONString(account);
    }

    /*************  mybatis ****************/

    @Override
    public List<Account> mFindAll() {

        return accountMapper.findAccountList();
    }

    @Override
    public Account mFindOne(int id) {
        return accountMapper.findAccount(id);
    }

    @Override
    public Account mxmlFindOne(int id) {
        return accountMapper2.selectById(id);
    }

    @Override
    public int mUpdateAccount(Account account) {

        return accountMapper.update(account.getName(),account.getMoney(),account.getId());
    }

    @Override
    public int mSave(Account account) {

        return accountMapper.add(account.getName(),account.getMoney());
    }

    @Override
    @Transactional
    public int transfer(double money) {
        //测试开启事物后的执行结果
        int a = accountMapper.update("七色花",100000 + money,5);
        int i = 1/0;
        int b = accountMapper.update("admin",100000 - money,4);

        return a + b;
    }
}
